<?php
namespace app\index\controller;

use app\index\model\passentranceguard;
use app\index\validate\EGCheck;
use think\Controller;
use think\Db;
use think\Request;

class Index extends  Controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
    }


    public function show(){

        return $this->fetch();
    }

    /*
     *
     * [用于在访客易软件监听到门禁刷卡记录之后，往微信预约系统发送门禁数据。]
     * @access  public
     * @author  lili
     * @param string token 验证信息
     * @param string card_num 门禁卡号
     * @param string person_name 人员姓名
     * @param string person_type 人员类别 1/2：访客、0：员工
     * @param string date 刷卡时间
     * @param string swipe_point 刷卡门点
     * @param string swipe_type 刷卡事件
     * @param string sn 设备SN码
     * @return json
     * @date  2018/9/18
     *
     *
     * */
    public function checkEntranceGuardQrcode(Request $request){

        if ($request->isPost()){

            cache('123456','123456');

            $send = input('post.',null,'htmlspecialchars,strip_tags');
            //验证token
            //var_dump(cache($send['token']));

            if(empty($send['token'])||empty(cache($send['token'])) ){
                codejson('201','token过期或者没有预约成功');
            }
            $token = $send['token'];
            $egcheckvalidate = new EGCheck();
            //验证接口参数
            if ($egcheckvalidate->check($send)){
                $data = $send;
                unset($data['token']);
                //开启事务
                Db::startTrans();
                try{
                    $ptgmodel = new passentranceguard();
                    $res = $ptgmodel->isUpdate(false)->save($data);
                    if ($res!=false&&is_numeric($res)&&$res>=1){
                        Db::commit();
                        //通过
                        codejson('200','验证成功，可以通过',$data);
                    }else{
                        Db::rollback();
                        codejson('203','数据插入失败或者异常');
                    }

                }catch (\Exception $e){
                    // 回滚事务
                    Db::rollback();
                }
            }else{
                //验证字段不通过
                codejson('202',$egcheckvalidate->getError());
            }
        }else{
            codejson('101','你的请求必须是post请求');
        }

    }

    /*
     * 返回二维码图片
     *
     *
     * */
    public function getQrcode(){

        vendor("phpqrcode.phpqrcode");
        $data ='http://localhost/EntranceGuard/public/index.php/checkqrcode';
        $outfile=ROOT_PATH."public/qrcode/".time().'.jpg';
        $level = 'L';
        $size =4;
        $QRcode = new \QRcode();
//        var_dump($QRcode);
        ob_start();
        $QRcode->png($data,$outfile,$level,$size,2);
        ob_end_clean();
        echo showImg($outfile);
        die();

    }




}
